﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSON.GeoCOM
{
    public enum MOT_LOCK_STATUS
    {
        MOT_LOCKED_OUT = 0, // locked out

        MOT_LOCKED_IN = 1, // locked in

        MOT_PREDICTION = 2 // prediction mode
    };
    public enum MOT_MODE
    {
        MOT_POSIT = 0, // configured for relative postioning

        MOT_OCONST = 1, // configured for constant speed

        MOT_MANUPOS = 2, // configured for manual positioning default setting

        MOT_LOCK = 3, // configured as "Lock-In"-controller

        MOT_BREAK = 4, // configured as "Brake"-controller do not use 5 and 6

        MOT_TERM = 7 // terminates the controller task
    };
    public enum MOT_STOPMODE
    {
        MOT_NORMAL = 0, // slow down with current acceleration

        MOT_SHUTDOWN = 1 // slow down by switch off power supply
    };
    public class MOT
    {
        public static RC ReadLockStatus(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,6021:\r\n"));
            responseString = responseString.Trim();
            //Status[long]  MOT_LOCK_STATUS
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC StartController(TsSerialPort com, MOT_MODE controlMode)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,6001:{0}\r\n",
                Convert.ToInt32(controlMode).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC StopController(TsSerialPort com, MOT_STOPMODE mode)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,6002:{0}\r\n",
                Convert.ToInt32(mode).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        /// <summary>
        /// 设置旋转速度
        /// </summary>
        /// <param name="com"></param>
        /// <param name="HzSpeed">单位为rad/s
        /// 1200,viva系列最高速为0.79rad/s
        /// TS30,TM30系列最高速为3.14rad/s</param>
        /// <param name="VSpeed">单位为rad/s</param>
        /// <returns></returns>
        public static RC SetVelocity(TsSerialPort com, double HzSpeed, double VSpeed)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,6004:{0},{1}\r\n",
                HzSpeed.ToString(), VSpeed.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }

        //public static RC FindPostion(TsSerialPort com, double Hz, double V)
        //{

        //}
    }
}
